package com.piesat.quake.business.device.service.impl;

import com.piesat.quake.business.device.model.ConfigurationUpdate;
import com.piesat.quake.business.device.model.DeviceConfigVO;
import com.piesat.quake.business.device.model.DeviceOptions;
import com.piesat.quake.business.device.model.NetworkData;
import com.piesat.quake.business.device.model.config.NtpData;
import com.piesat.quake.business.device.model.config.StationAndThreshold;
import com.piesat.quake.business.device.model.config.StationData;
import com.piesat.quake.business.device.model.config.ThresholdData;
import com.piesat.quake.business.device.service.DNetworkDataService;
import com.piesat.quake.business.device.service.DNtpDataService;
import com.piesat.quake.business.device.service.DPassagewayService;
import com.piesat.quake.business.device.service.DStationDataService;
import com.piesat.quake.business.device.service.DThresholdDataService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2023-03-13
 */
@Service
public class DeviceCommonDataService {
    @Autowired
    private DNetworkDataService networkDataService;
    @Autowired
    private DThresholdDataService thresholdDataService;
    @Autowired
    private DNtpDataService ntpDataService;
    @Autowired
    private DStationDataService stationDataService;
    @Autowired
    private DPassagewayService passagewayService;

    public DeviceConfigVO getRegionDeviceConfig(String regionCode) {
        NetworkData mqttConfig = networkDataService.getConfig(regionCode, ConfigurationUpdate.Network.MQTT);
        NetworkData ftpConfig = networkDataService.getConfig(regionCode, ConfigurationUpdate.Network.FTP);
        NetworkData ftpLogConfig = networkDataService.getConfig(regionCode, ConfigurationUpdate.Network.FTP_LOG);
        NetworkData updateConfig = networkDataService.getConfig(regionCode, ConfigurationUpdate.Network.UPDATE);
        DeviceConfigVO deviceOptions = new DeviceConfigVO();
        deviceOptions.setMqttConfig(mqttConfig);
        deviceOptions.setFtpConfig(ftpConfig);
        deviceOptions.setFtpLogConfig(ftpLogConfig);
        deviceOptions.setUpdateConfig(updateConfig);
        NtpData ntpData = ntpDataService.getById(regionCode);
        deviceOptions.setNtp(ntpData);
        ThresholdData thresholdData = thresholdDataService.getById(regionCode);
        deviceOptions.setThreshold(thresholdData);
        return deviceOptions;
    }

    public DeviceConfigVO getDeviceFullConfig(String id) {
        DeviceConfigVO deviceOptions = getRegionDeviceConfig(id);
        StationData stationData = stationDataService.getStationData(id);
        deviceOptions.setStationData(stationData);
        deviceOptions.setPassageways(stationDataService.getPassageways(id));
        return deviceOptions;
    }

    @Transactional(rollbackFor = Exception.class)
    public void saveConfig(DeviceOptions deviceOptions) {
        saveRegionDeviceConfig(deviceOptions);
        stationDataService.saveDeviceConfig(deviceOptions);
    }


    public void saveRegionDeviceConfig(DeviceOptions deviceOptions) {
        networkDataService.saveNetworkConfig(deviceOptions);
        NtpData ntp = deviceOptions.getNtp();
        ntp.setId(deviceOptions.getId());
        boolean b = ntpDataService.saveOrUpdate(ntp);
        if (!b) {
            throw new IllegalArgumentException("写入ntp信息失败");
        }
        StationAndThreshold stationAndThreshold = deviceOptions.getStationAndThreshold();
        ThresholdData thresholdData = stationAndThreshold.toThresholdData();
        thresholdData.setId(deviceOptions.getId());
        boolean save = thresholdDataService.saveOrUpdate(thresholdData);
        if (!save) {
            throw new IllegalArgumentException("写入阈值信息失败");
        }
    }

    public void updateConfig(ConfigurationUpdate configurationUpdate) {
        short configType = configurationUpdate.getConfigType();
        switch (configType) {
            case ConfigurationUpdate.NETWORK:
                ConfigurationUpdate.Network network = configurationUpdate.getNetwork();
                byte communicationType = network.getCommunicationType();
                NetworkData networkData = null;
                switch (communicationType) {
                    case ConfigurationUpdate.Network.MQTT:
                        networkData = network.getMqttConfig();
                        break;
                    case ConfigurationUpdate.Network.FTP:
                        networkData = network.getFtpConfig();
                        break;
                    case ConfigurationUpdate.Network.FTP_LOG:
                        networkData = network.getFtpLogConfig();
                        break;
                    case ConfigurationUpdate.Network.UPDATE:
                        networkData = network.getUpdateConfig();
                        break;
                }
                assert networkData != null;
                networkData.setId(configurationUpdate.getId());
                networkData.setType(communicationType);
                networkDataService.update(networkData);
                if (networkData.getPath() == null) {
                    networkData.setPath("");
                }
                if (networkData.getPassword() == null) {
                    networkData.setPassword("");
                }
                if (networkData.getUsername() == null) {
                    networkData.setUsername("");
                }
                break;
            case ConfigurationUpdate.STATION_INFO:
                StationData stationData = configurationUpdate.getStationData();
                stationData.setId(configurationUpdate.getId());
                stationDataService.update(stationData);
                break;
            case ConfigurationUpdate.THRESHOLD:
                ThresholdData threshold = configurationUpdate.getThreshold();
                threshold.setId(configurationUpdate.getId());
                thresholdDataService.update(threshold);
                break;
            case ConfigurationUpdate.PASSAGEWAY:
                ConfigurationUpdate.PassagewayConfig passageway = configurationUpdate.getPassageway();
                passagewayService.updatePassageways(passageway.getPassageways(), configurationUpdate.getId());
                break;
            case ConfigurationUpdate.NTP:
                NtpData ntp = configurationUpdate.getNtp();
                ntp.setId(configurationUpdate.getId());
                ntpDataService.update(ntp);
                break;
        }
    }

}
